package com.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.controller.vo.ShopsApplyVo;
import com.dao.ICustomerDao;
import com.dao.IRCustCommisShopsDao;
import com.dao.IShopsApplyDao;
import com.dao.entity.Customer;
import com.dao.entity.RCustCommisShops;
import com.dao.entity.ShopsApply;
import com.service.IShopsService;
import com.utils.commons.PageUtils;
import com.utils.enums.ENUM_SUCESS;

@Service
public class ShopsServiceImpl implements IShopsService {
	
	@Autowired
	private IShopsApplyDao shopsDao;
	
	@Autowired
	private ICustomerDao customerDao;
	
	@Autowired
	private IRCustCommisShopsDao rCustCommisShopsDao;

	@Override
	public ShopsApply saveShopsApply(ShopsApply shopsApply) {
		return shopsDao.save(shopsApply);
	}

	@Override
	public void completeShops(Long applyId) {
		shopsDao.findById(applyId).ifPresent(e ->{
			e.setIsComplete(ENUM_SUCESS.YES.value);
			shopsDao.save(e);
		});
	}

	@Override
	public Page<ShopsApplyVo> getShops(ShopsApply shops, Integer page, Integer size) {
		Pageable pageable = PageUtils.toPageable(page, size, Sort.Direction.DESC, "createTime");
		ExampleMatcher matcher = ExampleMatcher.matching()
				.withMatcher("topic", ExampleMatcher.GenericPropertyMatchers.exact())
				.withIgnorePaths("createdate","updatedate");// 忽略字段，即不管password是什么值都不加入查询条件
		Example<ShopsApply> example = Example.of(shops, matcher);
		Page<ShopsApply> page1 =  shopsDao.findAll(example, pageable);
		List<ShopsApplyVo> resultlist = new ArrayList<ShopsApplyVo>();
		page1.forEach(s -> {
			ShopsApplyVo vo = new ShopsApplyVo(s);
			resultlist.add(vo);
		});
		return new PageImpl<ShopsApplyVo>(resultlist, pageable, page1.getTotalElements());
	}

	@Override
	public ShopsApply doApply(ShopsApply apply) {
		if(apply.getCustomerId() == null) {
			Customer c = customerDao.findByAccount(apply.getPhone());
			if(ObjectUtils.isEmpty(c)) {
				Customer entity = new Customer();
				entity.setAccount(apply.getPhone());
				entity.setIdCard(apply.getIdCard());
				entity.setSex(apply.getSex());
				entity.setUserName(apply.getUserName());
				entity.setPassword("111");
				entity = customerDao.save(entity);
				apply.setCustomerId(entity.getId());
			}
		}else {
			customerDao.findById(apply.getCustomerId()).ifPresent(e ->{
				apply.setPhone(e.getAccount());
				apply.setIdCard(e.getIdCard());
				apply.setSex(e.getSex());
				apply.setUserName(e.getUserName());
			});
		}
		
		//保存客户-专员关联关系
		RCustCommisShops rcc = rCustCommisShopsDao.findByCustomerIdAndCommisId(apply.getCustomerId(), apply.getCommisId());
		if(rcc == null) {
			rcc = new RCustCommisShops(apply.getCustomerId(), apply.getCommisId());
			rCustCommisShopsDao.save(rcc);
		}
		
		//保存申请
		return saveShopsApply(apply);
	}

}
